package be.fomp.jeve.application.data.standard.skilltree;

import java.util.List;
import java.util.Map;

import org.jdom.Element;

import be.fomp.jeve.application.data.standard.SkillTree.Group.Skill;
import be.fomp.jeve.application.data.staticdata.Attribute;

final class SkillImpl implements Skill {
	static final long serialVersionUID = 1L;
	
	private Integer typeId;
	private String typeName;
	private Integer groupId;
	
	private String description;
	private Integer rank;
	
	private Map<Integer, Integer> requiredSkills;
	private Attribute primaryAttribute;
	private Attribute secondaryAttribute;
	
	private Map<String, Integer> bonuses;
	
	@SuppressWarnings("unchecked")
	SkillImpl(Element skillElement){
		this.typeId = Integer.valueOf(skillElement.getAttributeValue("typeID"));
		this.typeName = skillElement.getAttributeValue("typeName");
		this.groupId = Integer.valueOf(skillElement.getAttributeValue("groupID"));
		
		this.description = skillElement.getChildText("description");
		this.rank = Integer.valueOf(skillElement.getChildText("rank"));
		
		List<Element> rowSets = skillElement.getChildren("rowset");
		
		for(Element rowSet: rowSets)
		{	
		    //Setting the required skills
			if(rowSet.getAttribute("requiredSkills") != null){
				for(Element required: (List<Element>)rowSet.getChildren()){
					Integer id = Integer.valueOf(required.getAttributeValue("typeID"));
					Integer level = Integer.valueOf(required.getAttributeValue("skillLevel"));
					this.requiredSkills.put(id,level);
				}
			}
			//Setting the bonuses
			if(rowSet.getAttribute("skillBonusCollection") != null){
				for(Element bonus: (List<Element>)rowSet.getChildren()){
					String type = bonus.getAttributeValue("bonusType");
					Integer value = Integer.valueOf(bonus.getAttributeValue("bonusValue"));
					this.bonuses.put(type,value);
				}
			}
		}
		
		//FIXME possible conversion bug
		Element attributes = skillElement.getChild("requiredAttributes");
		this.primaryAttribute = Attribute.valueOf(attributes.getChildText("primaryAttribute"));
		this.secondaryAttribute = Attribute.valueOf(attributes.getChildText("secondaryAttribute"));
	}

	public Integer getTypeId() {return this.typeId;}
	public String getTypeName() {return this.typeName;}
	public Integer getGroupId() {return this.groupId;}
	public String getDescription() {return this.description;}
	public Integer getRank() {return this.rank;}
	
	public Map<Integer, Integer> getRequiredSkills() {return this.requiredSkills;}
	
	public Attribute getPrimaryAttribute() {return this.primaryAttribute;}
	public Attribute getSecondaryAttribute() {return this.secondaryAttribute;}

	public Map<String, Integer> getBonuses() {return this.bonuses;}
}
